package com.pig4cloud.pigx.contract.api.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.api.enums.AbnormalEnum;
import com.pig4cloud.pigx.common.customize.api.util.OpenApiUtil;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.contract.api.v1.service.OrderHistoryServiceV1;
import com.pig4cloud.pigx.contract.api.v1.utils.OpenApiContractUtilsV1;
import com.pig4cloud.pigx.contract.entity.ContractOrderHistory;
import com.pig4cloud.pigx.contract.enums.ContractOrderEnum;
import com.pig4cloud.pigx.contract.enums.ContractPositionEnum;
import com.pig4cloud.pigx.contract.mapper.ContractOrderHistoryMapper;
import com.pig4cloud.pigx.contract.open.api.dto.OpenApiOrderDTOV1;
import com.pig4cloud.pigx.contract.open.api.vo.OpenApiOrderVOV1;
import com.pig4cloud.pigx.contract.utils.ContractUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 */
@Service
@AllArgsConstructor
public class OrderHistoryServiceImplV1 extends ServiceImpl<ContractOrderHistoryMapper, ContractOrderHistory> implements OrderHistoryServiceV1 {

	private final OpenApiContractUtilsV1 apiContractUtils;

	private final ContractUtils contractUtils;

	/**
	 * 获取强平单历史
	 *
	 * @param dto dto
	 * @return r
	 */
	@Override
	public R<List<OpenApiOrderVOV1>> getLiquidateOrder(OpenApiOrderDTOV1 dto) {
		dto.setType(ContractOrderEnum.Type.LIQUIDATE.getValue());
		List<OpenApiOrderVOV1> list = this.getOrderList(dto);
		return R.ok(list);
	}

	/**
	 * 获取用户历史订单
	 *
	 * @param dto dto
	 * @return r
	 */
	@Override
	public R<List<OpenApiOrderVOV1>> getAllOrderList(OpenApiOrderDTOV1 dto) {
		List<OpenApiOrderVOV1> list = this.getOrderList(dto);
		return R.ok(list);
	}

	private List<OpenApiOrderVOV1> getOrderList(OpenApiOrderDTOV1 dto) {
		// 如果传入币对,则校验币对是否正确
		if (dto.getSymbolName() != null) {
			apiContractUtils.getSymbolMongo(dto.getSymbolName());
		}
		PigxUser user = apiContractUtils.getUser();
		try {
			// 查询
			List<ContractOrderHistory> orderList = baseMapper.selectList(new QueryWrapper<ContractOrderHistory>() {{
				if (dto.getSymbolName() != null) {
					eq("symbol_name", dto.getSymbolName());
				}
				if (dto.getStartTime() != null) {
					ge("create_time", Instant.ofEpochMilli(dto.getStartTime()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime());
				}
				if (dto.getEndTime() != null) {
					le("create_time", Instant.ofEpochMilli(dto.getEndTime()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime());
				}
				if (dto.getOrderId() != null) {
					eq("id", dto.getOrderId());
				}
				if (dto.getType() != null) {
					eq("type", dto.getType());
				}
				eq("user_id", user.getId());
				orderByDesc("create_time");
			}});
			// 遍历类型转换 字典翻译 精度调整
			List<OpenApiOrderVOV1> voList = new ArrayList<>();
			orderList.forEach(e -> {
				OpenApiOrderVOV1 vo = new OpenApiOrderVOV1();
				BeanUtils.copyProperties(e, vo);
				vo.setTime(e.getCreateTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
				vo.setType(ContractOrderEnum.Type.findName(vo.getType()));
				vo.setSide(ContractOrderEnum.Side.findName(vo.getSide()));
				vo.setPositionMode(ContractPositionEnum.Mode.findName(vo.getPositionMode()));
				vo.setStatus(ContractOrderEnum.Status.findName(vo.getStatus()));
				vo.setOperationType(ContractOrderEnum.OperationType.findName(vo.getOperationType()));
				contractUtils.setScaleBySymbol(vo, e.getSymbolName());
				voList.add(vo);
			});
			return voList;
		} catch (Exception e) {
			throw OpenApiUtil.getError(AbnormalEnum.MSG.DISCONNECTED);
		}
	}

}
